package com.qfedu.config;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName LoggerConfig
 * @Description TODO
 * @Author young
 * @Date 2022-09-27 21:07
 * @Version 1.0
 **/
@Aspect
@Component
public class LoggerConfig {
    private static final Logger logger = LoggerFactory.getLogger(LoggerConfig.class);

    @Pointcut(value = "execution(* *.*.service.impl.*.*(..))")
    private void pt1(){};

    @Around(value = "pt1()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint){
        //1,开始记录日志
        logger.debug("=================开始记录日志==================="+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        //2,记录时间+执行的方法
        MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
        logger.debug("当前执行的方法为: {}"+signature.getMethod().getName());
        Object proceed = null;
        try {
            proceed = proceedingJoinPoint.proceed();
            return proceed;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            logger.error("执行方法异常: {}"+throwable.getMessage());
        }finally {
            logger.debug("=================结束记录日志==================="+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            return proceed;
        }
    }
}
